package ar.com.utn.openqsb.lip.ui;

import java.text.DecimalFormat;

import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ar.com.utn.openqsb.lip.core.SimplexResult;
import ar.com.utn.openqsb.lip.core.Constraint.Relation;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class ExerciseResultPanel extends JPanel
{

    private static final long serialVersionUID = 1L;

    public ExerciseResultPanel(final SimplexResult result)
    {

        // design form
        FormLayout layout = buildLayout(result);
        PanelBuilder builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();
        JFormattedTextField valueField;
        JTextField textField;

        // Add decision variables
        builder.addLabel("Variables", cc.xy(1, 1));
        int y = 2;
        for (int i = 0; i < result.getDecisionVariableList().length; i++)
        {
            textField = new JTextField("X" + i);
            textField.setEditable(false);
            builder.add(textField, cc.xy(1, y));
            y++;
        }

        builder.addLabel("Solución", cc.xy(2, 1));
        y = 2;
        for (double dv : result.getDecisionVariableList())
        {
            valueField = new JFormattedTextField(DecimalFormat.getInstance());
            valueField.setEditable(false);
            valueField.setValue(dv);
            builder.add(valueField, cc.xy(2, y));
            y++;
        }
        builder.addLabel("Costo", cc.xy(3, 1));
        y = 2;
        for (double dv : result.getObjectiveCoefficientList())
        {
            valueField = new JFormattedTextField(DecimalFormat.getInstance());
            valueField.setEditable(false);
            valueField.setValue(dv);
            builder.add(valueField, cc.xy(3, y));
            y++;
        }

        builder.addLabel("Contribución", cc.xy(4, 1));
        y = 2;
        for (double dv : result.getVariableContributionList())
        {
            valueField = new JFormattedTextField(DecimalFormat.getInstance());
            valueField.setEditable(false);
            valueField.setValue(dv);
            builder.add(valueField, cc.xy(4, y));
            y++;
        }

        // Add Z row
        y = result.getDecisionVariableList().length + 2;
        textField = new JTextField("Z");
        textField.setEditable(false);
        builder.add(textField, cc.xy(1, y));
        valueField = new JFormattedTextField(DecimalFormat.getInstance());
        valueField.setEditable(false);
        valueField.setValue(result.getValue());
        builder.add(valueField, cc.xy(2, y));
        textField = new JTextField(result.getObjectiveType().name());
        textField.setEditable(false);
        builder.add(textField, cc.xy(3, y));

        // Add Slack column names
        y = result.getDecisionVariableList().length + 3;
        builder.addLabel("Restricción", cc.xy(1, y));
        y++;
        for (int i = 0; i < result.getSlackValueList().length; i++)
        {
            textField = new JTextField("S" + i);
            textField.setEditable(false);
            builder.add(textField, cc.xy(1, y));
            y++;
        }

        y = result.getDecisionVariableList().length + 3;
        builder.addLabel("Utilizado", cc.xy(2, y));
        y++;
        for (double dv : result.getSlackValueList())
        {
            valueField = new JFormattedTextField(DecimalFormat.getInstance());
            valueField.setEditable(false);
            valueField.setValue(dv);
            builder.add(valueField, cc.xy(2, y));
            y++;
        }

        y = result.getDecisionVariableList().length + 3;
        builder.addLabel("Dirección", cc.xy(3, y));
        y++;
        for (Relation r : result.getConstraintRelationList())
        {
            textField = new JTextField(r.toString());
            textField.setEditable(false);
            builder.add(textField, cc.xy(3, y));
            y++;
        }

        y = result.getDecisionVariableList().length + 3;
        builder.addLabel("Límite", cc.xy(4, y));
        y++;
        for (double dv : result.getConstraintRHSList())
        {
            valueField = new JFormattedTextField(DecimalFormat.getInstance());
            valueField.setEditable(false);
            valueField.setValue(dv);
            builder.add(valueField, cc.xy(4, y));
            y++;
        }
        // build the form
        add(builder.getPanel());

    }

    public FormLayout buildLayout(final SimplexResult result)
    {
        StringBuilder columns = new StringBuilder();
        int c = 5;
        while (c-- > 0)
        {
            columns.append("50dlu, ");
        }

        StringBuilder rows = new StringBuilder();
        int r = 4 + result.getDecisionVariableList().length + result.getSlackValueList().length;
        r--;
        while (r-- > 0)
        {
            rows.append("20dlu, ");
        }
        return new FormLayout(columns.toString(), rows.toString());
    }

}
